package com.woniuxy.rms.service.impl;

import com.woniuxy.rms.dao.*;
import com.woniuxy.rms.pojo.Admins;
import com.woniuxy.rms.pojo.Complain;
import com.woniuxy.rms.pojo.Mess;
import com.woniuxy.rms.pojo.Users;
import com.woniuxy.rms.service.*;
import com.woniuxy.rms.util.*;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Auther: ChenGang  <br/>
 * Date: 2020/3/30 0030:下午 5:49  <br/>
 * Description:投诉管理服务层
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ComplainServiceImpl implements ComplainService {

    @Autowired
    private ComplainMapper complainMapper;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private HotService hotService;

    @Autowired
    private MessMapper messMapper;

    @Autowired
    private DynamicService dynamicService;

    @Autowired
    private ServerService serverService;

    @Autowired
    private UsersMapper usersMapper;

    @Override
    public List<Complain> findByPage(Integer currentPage, Integer pageSize, Integer status, Integer complainType) {
        return complainMapper.findByPage(currentPage,pageSize,status,complainType);
    }

    @Override
    public DataResult complainType(Long id, Integer status) {
        //获取投诉信息
        Complain complain = complainMapper.selectByPrimaryKey(id);
        //获取投诉用户的邮箱
        Users users = usersMapper.selectByPrimaryKey(complain.getUserId());
        //如果不是处理通过，直接修改状态即可
        if(!status.equals(ComplainStatus.UNDISPOSE)){
            return DataResult.failed();
        }
        //处理中
        if(!status.equals(ComplainStatus.DISPOSING)){
            complainMapper.checkStatus(id,status);
            try {
                EmailUtil.sendMail(users.getEmail(),"尊敬的业主，您好！您的投诉正在处理中！");
            } catch (Exception e) {
                e.printStackTrace();
            }
            return DataResult.success();
        }
        return DataResult.success();
    }

    @Override
    public Complain findById(Long id) {
        return complainMapper.findById(id);
    }


    @Override
    public DataResult disposePass(Complain complain) {
        //获取投诉信息
        Complain co = complainMapper.selectByPrimaryKey(complain.getId());
        //获取投诉用户的邮箱
        Users users = usersMapper.selectByPrimaryKey(co.getUserId());
        //获取该投诉对象被投诉的次数，如果次数超过10，则对投诉对象进行处理
        Integer count = complainMapper.findCountByComplain(co.getComplain(),co.getComplainType());
        if (count > 10){
            //判断投诉类型
            //投诉动态
            if (co.getComplainType().equals(BusinessTypeUtil.DYNAMIC.getType())){
                //删除动态
                dynamicService.delById(co.getComplain());
            }
            //投诉商品
            if (co.getComplainType().equals(BusinessTypeUtil.GOODS.getType())){
                //下架商品
                goodsService.goodsStatus(co.getComplain(), CheckUtil.UNPASS);
            }
            //投诉活动
            if (co.getComplainType().equals(BusinessTypeUtil.HOT.getType())){
                //取消活动
                hotService.hotStatus(co.getComplain(), CheckUtil.UNPASS);
            }
            //投诉服务
            if (co.getComplainType().equals(BusinessTypeUtil.SERVER.getType())){
                serverService.checkStatus(co.getComplain(), CheckUtil.UNPASS);
            }
        }
        //更改投诉处理状态
        complainMapper.checkStatus(co.getId(),ComplainStatus.DISPOSED);
        //处理回复保存
        Admins admins = (Admins) SecurityUtils.getSubject().getPrincipal();
        Mess mess = new Mess();
        mess.setAcceptId(admins.getId());
        mess.setContent(complain.getReplay());
        mess.setAcceptRoleId(admins.getRoleId());
        mess.setUserId(co.getUserId());
        mess.setCreatTime(new Date());
        messMapper.insertSelective(mess);
        //处理成功后发送邮件通知
        try {
            EmailUtil.sendMail(users.getEmail(),"尊敬的业主，您好！您的投诉已处理完毕！");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return DataResult.success();
    }
}